{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Usage Guide\n",
    "\n",
    "In this sample piece, we will go through how to use the elliptical copulae as defined in the `copulae` package. We will not be covering what elliptical copula are. \n",
    "\n",
    "We will use the residuals data from the package for this tutorial. The data is a historical realization of the unknown processes. Each column follows their distinct (and unknown) process. However, these processes are related (have a dependency structure) to one another. Our task is to learn the dependency structure so that we could simulate future events.\n",
    "\n",
    "This example is essentially a stripped down case of the GARCH-Copula model, which is common in certain industries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "      <th>G</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.730967</td>\n",
       "      <td>0.530860</td>\n",
       "      <td>0.287320</td>\n",
       "      <td>1.193049</td>\n",
       "      <td>0.019040</td>\n",
       "      <td>1.100507</td>\n",
       "      <td>0.278214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.067853</td>\n",
       "      <td>-1.181313</td>\n",
       "      <td>-2.546173</td>\n",
       "      <td>0.381538</td>\n",
       "      <td>-0.038734</td>\n",
       "      <td>0.269874</td>\n",
       "      <td>-0.603940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.181835</td>\n",
       "      <td>0.380326</td>\n",
       "      <td>0.928632</td>\n",
       "      <td>-0.316861</td>\n",
       "      <td>0.106473</td>\n",
       "      <td>-0.324854</td>\n",
       "      <td>-0.447824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.445040</td>\n",
       "      <td>0.734531</td>\n",
       "      <td>-0.133299</td>\n",
       "      <td>-0.374091</td>\n",
       "      <td>0.173616</td>\n",
       "      <td>-0.319402</td>\n",
       "      <td>-0.775106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.296363</td>\n",
       "      <td>3.024053</td>\n",
       "      <td>0.815791</td>\n",
       "      <td>1.168521</td>\n",
       "      <td>0.134044</td>\n",
       "      <td>1.110424</td>\n",
       "      <td>1.705190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A                     B                    C                     \\\n",
       "0              0.730967             0.530860              0.287320   \n",
       "1              2.067853            -1.181313             -2.546173   \n",
       "2             -2.181835             0.380326              0.928632   \n",
       "3              0.445040             0.734531             -0.133299   \n",
       "4              0.296363             3.024053              0.815791   \n",
       "\n",
       "   D                    E                     F                            G  \n",
       "0             1.193049              0.019040              1.100507  0.278214  \n",
       "1             0.381538             -0.038734              0.269874 -0.603940  \n",
       "2            -0.316861              0.106473             -0.324854 -0.447824  \n",
       "3            -0.374091              0.173616             -0.319402 -0.775106  \n",
       "4             1.168521              0.134044              1.110424  1.705190  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from copulae.datasets import load_residuals\n",
    "\n",
    "residuals = load_residuals()\n",
    "residuals.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will use both the `GaussianCopula` and the `StudentCopula`. But let's first start off with the Gaussian copula."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gaussian Copula\n",
    "\n",
    "An alias of the `GaussianCopula` is `NormalCopula`. We can use either as they both refer to the same underlying structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from copulae import GaussianCopula\n",
    "\n",
    "_, ndim = residuals.shape\n",
    "g_cop = GaussianCopula(dim=ndim)  # initializing the copula\n",
    "g_cop.fit(residuals)  # fit the copula to the data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Internally, the `fit` method will convert the data to pseudo observations so there is no need to for that sort of data treatment prior. However, even if your data is already in pseudo observations, there will be no change to the results as the transformation is monotonic in nature. \n",
    "\n",
    "To understand the quality of the fit, you can use the `summary` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div>\n",
       "    <h2>Gaussian Copula Summary</h2>\n",
       "    <div>Gaussian Copula with 7 dimensions</div>\n",
       "    <hr/>\n",
       "    <div>\n",
       "        <h3>Parameters</h3>\n",
       "        <strong>Correlation Matrix</strong><table border=\"1\" class=\"dataframe\">\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.191081</td>\n",
       "      <td>-0.365944</td>\n",
       "      <td>0.128204</td>\n",
       "      <td>0.128853</td>\n",
       "      <td>0.110536</td>\n",
       "      <td>0.309972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.191081</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.512683</td>\n",
       "      <td>-0.027041</td>\n",
       "      <td>-0.082239</td>\n",
       "      <td>-0.032021</td>\n",
       "      <td>0.207898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>-0.365944</td>\n",
       "      <td>0.512683</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.058283</td>\n",
       "      <td>-0.006468</td>\n",
       "      <td>0.055127</td>\n",
       "      <td>0.010648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.128204</td>\n",
       "      <td>-0.027041</td>\n",
       "      <td>0.058283</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.624116</td>\n",
       "      <td>0.936115</td>\n",
       "      <td>0.590101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.128853</td>\n",
       "      <td>-0.082239</td>\n",
       "      <td>-0.006468</td>\n",
       "      <td>0.624116</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.711072</td>\n",
       "      <td>0.416072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.110536</td>\n",
       "      <td>-0.032021</td>\n",
       "      <td>0.055127</td>\n",
       "      <td>0.936115</td>\n",
       "      <td>0.711072</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.562437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.309972</td>\n",
       "      <td>0.207898</td>\n",
       "      <td>0.010648</td>\n",
       "      <td>0.590101</td>\n",
       "      <td>0.416072</td>\n",
       "      <td>0.562437</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "    </div>\n",
       "    \n",
       "<div>\n",
       "    <h3>Fit Summary</h2>\n",
       "    <hr/>\n",
       "    <table>\n",
       "        <tr><th colspan=\"2\">Fit Summary</th></tr>\n",
       "        <tr><td>Log Likelihood</td><td>-810.9309775288916</td></tr>\n",
       "        <tr><td>Variance Estimate</td><td>Not Implemented Yet</td></tr>\n",
       "        <tr><td>Method</td><td>Maximum pseudo-likelihood</td></tr>\n",
       "        <tr><td>Data Points</td><td>394</td></tr>\n",
       "    </table>\n",
       "    <br/>\n",
       "    <table>\n",
       "        <tr><th colspan=\"2\">Optimization Setup</th><th colspan=\"2\">Results</th></tr>\n",
       "        <tr><td>bounds</td><td>[(-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0)]</td><td>x</td><td>[ 0.1910806  -0.36594407  0.12820358  0.1288529   0.11053562  0.30997244\n",
       "  0.51268309 -0.02704128 -0.08223943 -0.03202084  0.20789778  0.0582833\n",
       " -0.0064678   0.05512652  0.01064777  0.62411582  0.93611501  0.59010119\n",
       "  0.71107239  0.41607167  0.56243693]</td></tr><tr><td>options</td><td>{'maxiter': 20000, 'ftol': 1e-06, 'iprint': 1, 'disp': False, 'eps': 1.5e-08}</td><td>fun</td><td>-810.9309775288916</td></tr><tr><td>method</td><td>SLSQP</td><td>jac</td><td>[-0.00166741 -0.00034864 -0.01106552  0.00116718  0.00198573  0.00135666\n",
       " -0.00127329  0.00070486 -0.00857957  0.00972401 -0.0005457   0.00277396\n",
       "  0.00683637 -0.01141416 -0.00187204  0.02055458 -0.05338734 -0.01230849\n",
       " -0.02126702 -0.00407757  0.01490055]</td></tr><tr><td>None</td><td>None</td><td>nit</td><td>26</td></tr><tr><td>None</td><td>None</td><td>nfev</td><td>657</td></tr><tr><td>None</td><td>None</td><td>njev</td><td>26</td></tr><tr><td>None</td><td>None</td><td>status</td><td>0</td></tr><tr><td>None</td><td>None</td><td>message</td><td>Optimization terminated successfully.</td></tr><tr><td>None</td><td>None</td><td>success</td><td>True</td></tr>\n",
       "    </table>\n",
       "</div>\n",
       "        \n",
       "</div>\n"
      ],
      "text/plain": [
       "Gaussian Copula Summary\n",
       "================================================================================\n",
       "Gaussian Copula with 7 dimensions\n",
       "\n",
       "\n",
       "Parameters\n",
       "--------------------------------------------------------------------------------\n",
       " Correlation Matrix \n",
       " 1.000000  0.191081 -0.365944  0.128204  0.128853  0.110536  0.309972\n",
       " 0.191081  1.000000  0.512683 -0.027041 -0.082239 -0.032021  0.207898\n",
       "-0.365944  0.512683  1.000000  0.058283 -0.006468  0.055127  0.010648\n",
       " 0.128204 -0.027041  0.058283  1.000000  0.624116  0.936115  0.590101\n",
       " 0.128853 -0.082239 -0.006468  0.624116  1.000000  0.711072  0.416072\n",
       " 0.110536 -0.032021  0.055127  0.936115  0.711072  1.000000  0.562437\n",
       " 0.309972  0.207898  0.010648  0.590101  0.416072  0.562437  1.000000\n",
       "\n",
       "\n",
       "\n",
       "                                  Fit Summary                                   \n",
       "================================================================================\n",
       "Log. Likelihood      : -810.9309775288916\n",
       "Variance Estimate    : Not Implemented Yet\n",
       "Method               : Maximum pseudo-likelihood\n",
       "Data Points          : 394\n",
       "\n",
       "Optimization Setup\n",
       "--------------------------------------------------------------------------------\n",
       "\tbounds         : [(-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0)]\n",
       "\toptions        : {'maxiter': 20000, 'ftol': 1e-06, 'iprint': 1, 'disp': False, 'eps': 1.5e-08}\n",
       "\tmethod         : SLSQP\n",
       "\n",
       "Results\n",
       "--------------------------------------------------------------------------------\n",
       "\tx              : [ 0.1910806  -0.36594407  0.12820358  0.1288529   0.11053562  0.30997244\n",
       "  0.51268309 -0.02704128 -0.08223943 -0.03202084  0.20789778  0.0582833\n",
       " -0.0064678   0.05512652  0.01064777  0.62411582  0.93611501  0.59010119\n",
       "  0.71107239  0.41607167  0.56243693]\n",
       "\tfun            : -810.9309775288916\n",
       "\tjac            : [-0.00166741 -0.00034864 -0.01106552  0.00116718  0.00198573  0.00135666\n",
       " -0.00127329  0.00070486 -0.00857957  0.00972401 -0.0005457   0.00277396\n",
       "  0.00683637 -0.01141416 -0.00187204  0.02055458 -0.05338734 -0.01230849\n",
       " -0.02126702 -0.00407757  0.01490055]\n",
       "\tnit            : 26\n",
       "\tnfev           : 657\n",
       "\tnjev           : 26\n",
       "\tstatus         : 0\n",
       "\tmessage        : Optimization terminated successfully.\n",
       "\tsuccess        : True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_cop.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PDF, CDF and Random Variates\n",
    "\n",
    "All the `pdf`, `cdf` and `random` methods of every copula work in the same manner. The only thing to note is that the input data for `pdf` and `cdf` must match the dimensions of the copula. In this case, we generate a 2x7 matrix, notice that the second dimension matches the dimension of the copula."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "random_matrix = np.random.uniform(0, 1, size=(2, 7))\n",
    "\n",
    "pdf = g_cop.pdf(random_matrix)  # length 2 ndarray\n",
    "cdf = g_cop.cdf(random_matrix)  # length 2 ndarray\n",
    "rv = g_cop.random(2)  # shape is 2 by 7. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reading Parameters\n",
    "\n",
    "All copulas are parameterized in their own ways. Archimedeans, for example, is parameterized by a single $\\theta$. For the Gaussian Copula, it is parameterized by the correlation matrix. To get the parameters for the copula, use the `params` property."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.1910806 , -0.36594407,  0.12820358,  0.1288529 ,  0.11053562,\n",
       "        0.30997244,  0.51268309, -0.02704128, -0.08223943, -0.03202084,\n",
       "        0.20789778,  0.0582833 , -0.0064678 ,  0.05512652,  0.01064777,\n",
       "        0.62411582,  0.93611501,  0.59010119,  0.71107239,  0.41607167,\n",
       "        0.56243693])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g_cop.params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case, we get a vector instead of a correlation matrix (even though I mentioned that Gaussian Copulas are parameterized by the correlation matrix!). The answer is simple, these numbers are actually the diagonal elements of the correlation matrix. After all, in a correlation matrix, only the elements in the diagonals are \"unique\". \n",
    "\n",
    "For elliptical copulas, to see the correlation matrix, use the `sigma` property."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.1910806 , -0.36594407,  0.12820358,  0.1288529 ,  0.11053562,  0.30997244],\n",
       "       [ 0.1910806 ,  1.        ,  0.51268309, -0.02704128, -0.08223943, -0.03202084,  0.20789778],\n",
       "       [-0.36594407,  0.51268309,  1.        ,  0.0582833 , -0.0064678 ,  0.05512652,  0.01064777],\n",
       "       [ 0.12820358, -0.02704128,  0.0582833 ,  1.        ,  0.62411582,  0.93611501,  0.59010119],\n",
       "       [ 0.1288529 , -0.08223943, -0.0064678 ,  0.62411582,  1.        ,  0.71107239,  0.41607167],\n",
       "       [ 0.11053562, -0.03202084,  0.05512652,  0.93611501,  0.71107239,  1.        ,  0.56243693],\n",
       "       [ 0.30997244,  0.20789778,  0.01064777,  0.59010119,  0.41607167,  0.56243693,  1.        ]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(linewidth=120)\n",
    "g_cop.sigma"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Overwriting Parameters\n",
    "\n",
    "The parameters are fit according to the empirical data. Many times, this is fine. However, there are instances where we want to overwrite the fitted parameters due to better understanding of the domain problem and any other reasons.\n",
    "\n",
    "The basic way is to overwrite via the `params` property setter as seen in the example below.\n",
    "\n",
    "```python\n",
    "cop.params = 123\n",
    "```\n",
    "\n",
    "However, for the elliptical copulas, we have a convenience function that makes it easier to work with correlation matrix."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To overwrite single elements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.5       , -0.36594407,  0.12820358,  0.1288529 ,  0.11053562,  0.30997244],\n",
       "       [ 0.5       ,  1.        ,  0.51268309, -0.02704128, -0.08223943, -0.03202084,  0.20789778],\n",
       "       [-0.36594407,  0.51268309,  1.        ,  0.0582833 , -0.0064678 ,  0.05512652,  0.01064777],\n",
       "       [ 0.12820358, -0.02704128,  0.0582833 ,  1.        ,  0.62411582,  0.93611501,  0.59010119],\n",
       "       [ 0.1288529 , -0.08223943, -0.0064678 ,  0.62411582,  1.        ,  0.71107239,  0.41607167],\n",
       "       [ 0.11053562, -0.03202084,  0.05512652,  0.93611501,  0.71107239,  1.        ,  0.56243693],\n",
       "       [ 0.30997244,  0.20789778,  0.01064777,  0.59010119,  0.41607167,  0.56243693,  1.        ]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert g_cop[0, 1] == g_cop[1, 0]\n",
    "g_cop[0, 1] = 0.5\n",
    "g_cop.sigma"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To overwrite an entire correlation matrix, follow the code snippet below:\n",
    "\n",
    "```python\n",
    "my_corr_mat = # some correlation matrix\n",
    "g_cop[:] = my_corr_mat  # this overwrites the entire correlation matrix\n",
    "```\n",
    "\n",
    "Behind the scenes, after overwriting the parameters, some transformations will be done to ensure that the resulting matrix remains positive semi-definite. If the matrix is already positive semi-definite, nothing will be done. But if it isn't, there will be some shifts to ensure that the resulting matrix has the minimum difference from the original matrix whilst being positive semi-definite. Thus don't be surprised if you change an element and notice that there are some bumps to the numbers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Student Copula\n",
    "\n",
    "An alias of the `StudentCopula` is `TCopula`. We can use either as they both refer to the same underlying structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from copulae import StudentCopula\n",
    "\n",
    "degrees_of_freedom = 5.5  # some random number, unnecessary to specify df but done for demonstration purposes\n",
    "t_cop = StudentCopula(dim=ndim, df=degrees_of_freedom)\n",
    "t_cop.fit(residuals)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Student Copula differs from the Gaussian Copula in that it has one additonal parameter, the degrees of freedom `df`. This can be seen from the summary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div>\n",
       "    <h2>Student Copula Summary</h2>\n",
       "    <div>Student Copula with 7 dimensions</div>\n",
       "    <hr/>\n",
       "    <div>\n",
       "        <h3>Parameters</h3>\n",
       "        <strong>Degree of Freedom</strong><span>10.544548100216232</span><br/><strong>Correlation Matrix</strong><table border=\"1\" class=\"dataframe\">\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.177449</td>\n",
       "      <td>-0.374352</td>\n",
       "      <td>0.092292</td>\n",
       "      <td>0.111153</td>\n",
       "      <td>0.071978</td>\n",
       "      <td>0.265016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.177449</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.525289</td>\n",
       "      <td>-0.055004</td>\n",
       "      <td>-0.077389</td>\n",
       "      <td>-0.065854</td>\n",
       "      <td>0.181235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>-0.374352</td>\n",
       "      <td>0.525289</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.057957</td>\n",
       "      <td>0.013242</td>\n",
       "      <td>0.060967</td>\n",
       "      <td>0.018375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.092292</td>\n",
       "      <td>-0.055004</td>\n",
       "      <td>0.057957</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.630144</td>\n",
       "      <td>0.939848</td>\n",
       "      <td>0.579629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.111153</td>\n",
       "      <td>-0.077389</td>\n",
       "      <td>0.013242</td>\n",
       "      <td>0.630144</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.716692</td>\n",
       "      <td>0.411570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.071978</td>\n",
       "      <td>-0.065854</td>\n",
       "      <td>0.060967</td>\n",
       "      <td>0.939848</td>\n",
       "      <td>0.716692</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.558957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0.265016</td>\n",
       "      <td>0.181235</td>\n",
       "      <td>0.018375</td>\n",
       "      <td>0.579629</td>\n",
       "      <td>0.411570</td>\n",
       "      <td>0.558957</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "    </div>\n",
       "    \n",
       "<div>\n",
       "    <h3>Fit Summary</h2>\n",
       "    <hr/>\n",
       "    <table>\n",
       "        <tr><th colspan=\"2\">Fit Summary</th></tr>\n",
       "        <tr><td>Log Likelihood</td><td>-838.7958878924677</td></tr>\n",
       "        <tr><td>Variance Estimate</td><td>Not Implemented Yet</td></tr>\n",
       "        <tr><td>Method</td><td>Maximum pseudo-likelihood</td></tr>\n",
       "        <tr><td>Data Points</td><td>394</td></tr>\n",
       "    </table>\n",
       "    <br/>\n",
       "    <table>\n",
       "        <tr><th colspan=\"2\">Optimization Setup</th><th colspan=\"2\">Results</th></tr>\n",
       "        <tr><td>bounds</td><td>[(0.0, inf), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0)]</td><td>x</td><td>[10.5445481   0.17744906 -0.37435175  0.09229179  0.11115308  0.07197828  0.26501573  0.525289   -0.05500438\n",
       " -0.07738919 -0.06585391  0.18123546  0.05795655  0.01324215  0.06096713  0.01837462  0.63014357  0.93984772\n",
       "  0.57962869  0.71669228  0.41157006  0.55895671]</td></tr><tr><td>options</td><td>{'maxiter': 20000, 'ftol': 1e-06, 'iprint': 1, 'disp': False, 'eps': 1.5e-08}</td><td>fun</td><td>-838.7958878924677</td></tr><tr><td>method</td><td>SLSQP</td><td>jac</td><td>[ 0.00017432  0.00118992 -0.00216763  0.0095118   0.00413062 -0.01389253  0.00140214  0.00169014 -0.00523717\n",
       " -0.00253901  0.00823851 -0.00204636  0.00958759  0.00623762 -0.01572668  0.00230405  0.00212973  0.00072002\n",
       " -0.0117249  -0.0012354  -0.00203878  0.01228576]</td></tr><tr><td>None</td><td>None</td><td>nit</td><td>32</td></tr><tr><td>None</td><td>None</td><td>nfev</td><td>827</td></tr><tr><td>None</td><td>None</td><td>njev</td><td>32</td></tr><tr><td>None</td><td>None</td><td>status</td><td>0</td></tr><tr><td>None</td><td>None</td><td>message</td><td>Optimization terminated successfully.</td></tr><tr><td>None</td><td>None</td><td>success</td><td>True</td></tr>\n",
       "    </table>\n",
       "</div>\n",
       "        \n",
       "</div>\n"
      ],
      "text/plain": [
       "Student Copula Summary\n",
       "================================================================================\n",
       "Student Copula with 7 dimensions\n",
       "\n",
       "\n",
       "Parameters\n",
       "--------------------------------------------------------------------------------\n",
       " Degree of Freedom  : 10.544548100216232\n",
       "\n",
       " Correlation Matrix \n",
       " 1.000000  0.177449 -0.374352  0.092292  0.111153  0.071978  0.265016\n",
       " 0.177449  1.000000  0.525289 -0.055004 -0.077389 -0.065854  0.181235\n",
       "-0.374352  0.525289  1.000000  0.057957  0.013242  0.060967  0.018375\n",
       " 0.092292 -0.055004  0.057957  1.000000  0.630144  0.939848  0.579629\n",
       " 0.111153 -0.077389  0.013242  0.630144  1.000000  0.716692  0.411570\n",
       " 0.071978 -0.065854  0.060967  0.939848  0.716692  1.000000  0.558957\n",
       " 0.265016  0.181235  0.018375  0.579629  0.411570  0.558957  1.000000\n",
       "\n",
       "\n",
       "\n",
       "                                  Fit Summary                                   \n",
       "================================================================================\n",
       "Log. Likelihood      : -838.7958878924677\n",
       "Variance Estimate    : Not Implemented Yet\n",
       "Method               : Maximum pseudo-likelihood\n",
       "Data Points          : 394\n",
       "\n",
       "Optimization Setup\n",
       "--------------------------------------------------------------------------------\n",
       "\tbounds         : [(0.0, inf), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0), (-1.0, 1.0)]\n",
       "\toptions        : {'maxiter': 20000, 'ftol': 1e-06, 'iprint': 1, 'disp': False, 'eps': 1.5e-08}\n",
       "\tmethod         : SLSQP\n",
       "\n",
       "Results\n",
       "--------------------------------------------------------------------------------\n",
       "\tx              : [10.5445481   0.17744906 -0.37435175  0.09229179  0.11115308  0.07197828  0.26501573  0.525289   -0.05500438\n",
       " -0.07738919 -0.06585391  0.18123546  0.05795655  0.01324215  0.06096713  0.01837462  0.63014357  0.93984772\n",
       "  0.57962869  0.71669228  0.41157006  0.55895671]\n",
       "\tfun            : -838.7958878924677\n",
       "\tjac            : [ 0.00017432  0.00118992 -0.00216763  0.0095118   0.00413062 -0.01389253  0.00140214  0.00169014 -0.00523717\n",
       " -0.00253901  0.00823851 -0.00204636  0.00958759  0.00623762 -0.01572668  0.00230405  0.00212973  0.00072002\n",
       " -0.0117249  -0.0012354  -0.00203878  0.01228576]\n",
       "\tnit            : 32\n",
       "\tnfev           : 827\n",
       "\tnjev           : 32\n",
       "\tstatus         : 0\n",
       "\tmessage        : Optimization terminated successfully.\n",
       "\tsuccess        : True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t_cop.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StudentParams(df=10.544548100216232, rho=array([ 0.17744906, -0.37435175,  0.09229179,  0.11115308,  0.07197828,  0.26501573,  0.525289  , -0.05500438,\n",
       "       -0.07738919, -0.06585391,  0.18123546,  0.05795655,  0.01324215,  0.06096713,  0.01837462,  0.63014357,\n",
       "        0.93984772,  0.57962869,  0.71669228,  0.41157006,  0.55895671]))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t_cop.params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The rest of the `StudentCopula` work in the same way as the `GaussianCopula`. The only thing to note is that to change the degrees of freedom, you use `t_cop.df = 5`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "That's all folks. We've gone through a simple example on \n",
    "\n",
    "1. How to fit a copula\n",
    "2. How to get a summary of the fitted copula\n",
    "3. How to get the PDF, CDF and Random Variates (these can be done even before fitting provided you set the parameters of the copula manually)\n",
    "4. How to overwrite parameters of the copula\n",
    "\n",
    "All the copulas pretty much follow a similar API so you probaby know about all of them already."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
